package com.game.message.task;

import com.game.message.Msg;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Created by lizhiwei on 2016/6/17.
 */
@Component
@Scope(value = "prototype")
public class ProcessorQueueEvent implements QueueEvent {
	private Logger logger = LoggerFactory.getLogger(ProcessorQueueEvent.class);
	private Msg msg;
	private ChannelHandlerContext ctx;
	private IProcessor processor;
	@Autowired
	private ProcessorManager processorManager;

	public ProcessorQueueEvent() {
	}

	public boolean execute() {
		if (this.processor == null) {
			processor = processorManager.getProcess(msg.getMsgId());
		}
		if (processor == null) {
			logger.warn("未找到对应的消息处理processor, 消息id:{}", msg.getMsgId());
			return true;
		}
		try {
			processor.process(msg,ctx);
		} catch (Exception ex) {
			logger.warn("消息处理processor异常, 消息id:{}", msg.getMsgId(),ex);
			return false;
		}
		return false;
	}

	public void setRequest(Msg msg) {
		this.msg = msg;
	}

	public void setCtx(ChannelHandlerContext ctx) {
		this.ctx = ctx;
	}

	public void setProcessor(IProcessor processor) {
		this.processor = processor;
	}
}
